package com.ivr.cm.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.ivr.cm.dao.ILimitFeeDao;
import com.ivr.cm.service.BaseServiceIF;
import com.ivr.cm.util.BaseUtil;
import com.ivr.cm.vo.LimitFeeVo;
import com.ivr.cm.vo.UserUsedVo;

/**
 * 日月限制数据实现
 * @author liugeng
 * @date 2015-12-15
 * @version 1.0
 *
 */
public class LimitFeeDaoImpl extends JdbcDaoSupport implements ILimitFeeDao, BaseServiceIF<Object> {
	private static Logger logger = Logger.getLogger(LimitFeeDaoImpl.class);

	@Override
	public int doProcess(Object obj) {
		if(obj instanceof LimitFeeVo){
			return insertLimitFee((LimitFeeVo)obj);
		}
		return 0;
	}
	/**根据主叫和被叫获得日月限制VO*/
	@Override
	public UserUsedVo getUserUsedVo(String mobile, String sp_code) {
		/**对被叫号码进行处理*/
		if(!"".equals(sp_code)){
			sp_code = sp_code.substring(0,9);
		}
		UserUsedVo userUsedVo = new UserUsedVo();
		try{
			/**获得当前日和月*/
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
			Calendar calendar = Calendar.getInstance();
			String day = format.format(calendar.getTime());
			String month = day.substring(0,6);
			
			/**查询日使用数量*/
			String sql = "select IFNULL(sum(fee_minute),0) as feeminute from tb_ivr_limit_fee where sp_code = ? and fee_day = ?";
			Object[] paramDayMinute = new Object[]{sp_code, day};
			logger.info(String.format("查询当日已用分钟数[%s]", BaseUtil.logSQL(sql, paramDayMinute)));
			int day_used_minute = this.getJdbcTemplate().queryForInt(sql, paramDayMinute);
			/**查询月使用情况*/
			sql = "select IFNULL(sum(fee_minute),0) as feeminute from tb_ivr_limit_fee where sp_code = ? and fee_month = ?";
			Object[] paramMonthMinute = new Object[]{sp_code, month};
			logger.info(String.format("查询当月已用分钟数[%s]", BaseUtil.logSQL(sql, paramMonthMinute)));
			int month_used_minute = this.getJdbcTemplate().queryForInt(sql,paramMonthMinute);
			
			/**查询日使用数量*/
			sql = "select IFNULL(ceiling(sum(fee_money/100)),0) as feemoney from tb_ivr_limit_fee where mobile = ? and fee_day = ?";
			Object[] paramDayMoney = new Object[]{mobile, day};
			logger.info(String.format("查询当日已用金额数[%s]", BaseUtil.logSQL(sql, paramDayMoney)));
			int day_used_money = this.getJdbcTemplate().queryForInt(sql, paramDayMoney);
			
			/**查询月使用情况*/
			sql = "select IFNULL(ceiling(sum(fee_money/100)),0) as feemoney from tb_ivr_limit_fee where mobile = ? and fee_month = ?";
			Object[] paramMonthMoney = new Object[]{mobile,month};
			logger.info(String.format("查询当月已用金额数[%s]", BaseUtil.logSQL(sql, paramMonthMoney)));
			int month_used_money = this.getJdbcTemplate().queryForInt(sql,paramMonthMoney);
			
			/**查询日使用次数*/
			sql = "select count(0) as feenum from tb_ivr_limit_fee where mobile = ? and fee_day = ?";
			Object[] paramDayNum = new Object[]{mobile, day};
			logger.info(String.format("查询当日已用次数[%s]", BaseUtil.logSQL(sql, paramDayNum)));
			int day_used_num = this.getJdbcTemplate().queryForInt(sql, paramDayNum);
			
			
			userUsedVo.setDay_used_minute(day_used_minute);
			userUsedVo.setMonth_used_minute(month_used_minute);
			userUsedVo.setDay_used_money(day_used_money);
			userUsedVo.setMonth_used_money(month_used_money);
			userUsedVo.setDay_used_num(day_used_num);
			
			return userUsedVo;
		}catch (Exception e) {
			logger.error(String.format("[%s][%s]查询日月限异常:%s", mobile,sp_code,ExceptionUtils.getFullStackTrace(e)));
		}
		return null;
	}
	/**根据主叫和被叫长号码获得用户使用的日金额和月金额*/
	@Override
	public UserUsedVo getUserUsedVoBySpNumber(String mobile, String sp_number) {
		UserUsedVo userUsedVo = new UserUsedVo();
		try{
			/**获得当前日和月*/
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
			Calendar calendar = Calendar.getInstance();
			String day = format.format(calendar.getTime());
			String month = day.substring(0,6);
			
			/**查询日使用数量*/
			String sql = "select IFNULL(ceiling(sum(fee_number/100)),0) as fee_number from tb_ivr_user_bill where mobile = ? and sp_number = ? and date_format(add_date,'%Y%m%d') = ?";
			Object[] paramDayMoney = new Object[]{mobile, sp_number, day};
			logger.info(String.format("查询当日已用金额数[%s]", BaseUtil.logSQL(sql, paramDayMoney)));
			int day_used_money = this.getJdbcTemplate().queryForInt(sql, paramDayMoney);
			
			/**查询月使用情况*/
			sql = "select IFNULL(ceiling(sum(fee_number/100)),0) as fee_number from tb_ivr_user_bill where mobile = ? and sp_number = ? and date_format(add_date,'%Y%m') = ?";
			Object[] paramMonthMoney = new Object[]{mobile, sp_number, month};
			logger.info(String.format("查询当月已用金额数[%s]", BaseUtil.logSQL(sql, paramMonthMoney)));
			int month_used_money = this.getJdbcTemplate().queryForInt(sql,paramMonthMoney);
			
			userUsedVo.setDay_used_money(day_used_money);
			userUsedVo.setMonth_used_money(month_used_money);
			
			return userUsedVo;
		}catch (Exception e) {
			logger.error(String.format("[%s][%s]查询日月限异常:%s", mobile, sp_number, ExceptionUtils.getFullStackTrace(e)));
		}
		return null;
	}
	
	/**保存IVR日月限制信息*/
	@Override
	public int insertLimitFee(LimitFeeVo limitFeeVo) {
		try{
			String sql = "insert into tb_ivr_limit_fee(mobile,province,city,sp_code,fee_minute,fee_money,fee_day,fee_month,time_begin,time_end,linkId) values(?,?,?,?,?,?,?,?,?,?,?)";
			Object[] param = new Object[]{limitFeeVo.getMobile(),limitFeeVo.getProvince(),
										limitFeeVo.getCity(),limitFeeVo.getSp_code(),
										limitFeeVo.getFee_minute(),limitFeeVo.getFee_money(),
										limitFeeVo.getFee_day(),limitFeeVo.getFee_month(),
										limitFeeVo.getTime_begin(),limitFeeVo.getTime_end(),
										limitFeeVo.getLinkId()};
			logger.info(String.format("[%s][%s]保存IVR计费记录[%s]", limitFeeVo.getMobile(),limitFeeVo.getSp_code(), BaseUtil.logSQL(sql, param)));
			int count = this.getJdbcTemplate().update(sql, param);
			
			return count;
		}catch(Exception e){
			logger.error(String.format("[%s][%s]保存IVR计费记录异常%s", limitFeeVo.getMobile(),limitFeeVo.getSp_code(),ExceptionUtils.getFullStackTrace(e)));
		}
		
		return 0;
	}
	
}
